package com.demo.demo1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * 转账案例业务层实现类
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    // 注入事务管理的模板
    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private AccountDao accountDao;
    public void transfer(final String out, final String in, final Double money) {
        // accountDao.outMoney(out, money);
        // accountDao.inMoney(in, money);
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                accountDao.outMoney(out, money);
                // int i = 1 / 0;
                accountDao.inMoney(in, money);
            }
        });
    }
}
